﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryCancer
{
    class listexemple
    {
        String[] listString;
        String[] apprentissage;
        String[] generalisation;
        int nbExemple;
        public int nbApprentissage;
        public int nbGeneralisation;
        double[] listErreur;

        public listexemple(string filename)
        {
            listErreur = new double[4];
            listErreur[0] = 0;
            listErreur[1] = 0;
            listErreur[2] = 0;
            listErreur[3] = 0;
            try
            {
                liretext(filename);
                nbExemple = listString.Length;
                nbApprentissage = Convert.ToInt32(nbExemple * 0.8);

                nbGeneralisation = Convert.ToInt32(nbExemple * 0.2);
                //nbExemple = new int(listString.Length * 0.8);

                apprentissage = new string[nbApprentissage];
                for (int i = 0; i < nbApprentissage; i++)
                {
                    apprentissage[i] = listString[i];
                }
                generalisation = new string[nbGeneralisation];
                for (int t = 0; t < nbGeneralisation; t++)
                {
                    generalisation[t] = listString[t + nbApprentissage];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("fail : "+ ex);
            }
        }
        private void liretext(String filename)
        {
            listString = File.ReadAllLines(@filename);
           listString= RandomizeStrings(listString);
        }
        public  string[] RandomizeStrings(string[] arr)
        {
            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            foreach (string s in arr)
            {
                list.Add(new KeyValuePair<int, string>(neurone.generator.Next(), s));
            }
            var sorted = from item in list
                         orderby item.Key
                         select item;
            string[] result = new string[arr.Length];
            int index = 0;
            foreach (KeyValuePair<int, string> pair in sorted)
            {
                result[index] = pair.Value;
                index++;
            }
            return result;
        }
        public double[] getInfoApprentissage(int numAsked)
        {
            double[] listResult = new double[4];
            string[] subjectString = new String[4];
            string subject;

            if (numAsked < nbApprentissage)
            {
                subjectString = apprentissage[numAsked].Split(new Char[] { ' ', ',', '.', ':', '\t' });
                for (int i = 0; i < 4; i++)
                {
                    subject = subjectString[i];
                    listResult[i] = Int32.Parse(subject);
                }
                listResult[3] -= 1;
                return listResult;
            }
            else
            {
                Console.WriteLine("ce chiffre n'existe pas");
                return listErreur;
            }
        }
        public double[] getInfoGeneralisation(int numAsked)
        {
            double[] listResult = new double[4];
            string[] subjectString = new String[4];
            string subject;

            if (numAsked < nbGeneralisation)
            {
                subjectString = generalisation[numAsked].Split(new Char[] { ' ', ',', '.', ':', '\t' });
                for (int i = 0; i < 4; i++)
                {
                    subject = subjectString[i];
                    listResult[i] = Int32.Parse(subject);
                }
                listResult[3] -= 1;

                return listResult;
            }
            else
            {
                Console.WriteLine("ce chiffre n'existe pas");
                return listErreur;
            }
        }
    }
}
